<?php
class PassportService extends Service {
	var $cookie_key	=	'AUTH_CODE';
	var $method		=	'LocalAuth';
    /**
     +----------------------------------------------------------
     * 架构函数
     +----------------------------------------------------------
     * @author melec制作
     * @access public
     +----------------------------------------------------------
     */
    public function __construct($method='') {
		//1.判断是否安装、是否运行该服务，系统服务可以不做判断
		//2.服务初始化
		//$this->init();
		//$this->run();
		//unset($_SESSION);

		//初始化认证方法
		if(!$method){
			$this->method	=	C('AUTH_METHOD');
		}else{
			$this->method	=	$method;
		}
	}

	// 服务初始化
	public function init(){
		return;
	}

	// 运行服务，系统服务自动运行
	public function run(){
		return;
	}

	// 验证用户是否已登录
	public function isLogged(){

		$_session_user		=	t($_SESSION['uname']);
		$_session_user_id	=	intval($_SESSION['mid']);

		// 验证本地系统登录
		if($_session_user_id > 0 ) {
			return true;
		}else if($_session_user_id==0){
			$username	=	jiemi($_cookie_user);
			return $this->loginLocal($username);
		// 如果未登录
		}else{
			return false;
		}
	}

	// 认证，记录Cookie登录
	public function authentication($username,$password) {

		//执行本地认证方法
		if($this->method=='LocalAuth'){

			return $this->loginLocal($username,$password,true);

		//执行第三方认证方法
		}else{

			$authservice	=	service($this->method);
			//获取认证状态
			$remote_check_status	=	$authservice->checkPassword($username,$password);

			if($remote_check_status===true){
				return $this->loginLocal($username,$password,true);
			}else{
				return $check_status;
			}
		}
	}

	//本地账号是否存在
	public function getLocalUser($username,$password=false){
		$map['uname']		=	$username;
		//if($password){
		//	$map['passwd']	=	md5($password);
		//}
		$user	=	model('User')->where($map)->find();
		if(!$user){
			return false;
		}else{
			return $user;
		}
	}

	// 注册账号到本地数据库
	public function regLocal($username,$password=false,$info){

		// 验证用户名、密码规则

		// 填充注册信息
		$info['uname']		=	$username;
		$info['passwd']		=	$password ? md5($password) : md5('huawei');
		$info['name']		=	$info['name'] ? $info['name'] : $username;
		$info['email']		=	$info['email'] ? $info['email'] : $username.'@notesmail.huawei.com';
		$info['fullname']	=	$info['fullname'] ? $info['fullname'] : $username;
		$info['regip']		=	get_client_ip();
		$info['regtime']	=	time();
		$info['active']		=	1;

		// 注册到本地数据库表
		$user	=	model('User');
		$result	=	$user->add($info);
		if(!$result){
			return false;
		}else{
			$info['uid']	=	$result;
			return $info;
		}
	}

	// 登陆到本地
	public function loginLocal($username,$password=false,$auto_reg=false,$info=''){

		// 获取账号信息
		$user	=	$this->getLocalUser($username,$password);

		// 自动注册不存在的账号
		if($auto_reg && !$user){
			$user	=	$this->regLocal($username,$password,$info);
		}

		// 再次验证,失败则返回
		if(!$user) {
			//$this->logoutLocal($username);
			return false;
		}else{

			// 注册session
			$_SESSION['mid']		=	$user['uid'];
			$_SESSION['uname']		=	$user['uname'];

			$cookie_code			=	jiami($user['uname']);

			//记录cookie
			cookie( $this->cookie_key, $cookie_code, (3600*24*365) );

			//记录登录日志,uid,uname,logintime
			//$logkey	=	'user='.$user['uid'].'||time='.time();
			//model('Xlog')->put('login:'.$logkey);

			//初始化用户资料
			$count	=	M('Space')->where("uid='$_SESSION[mid]'")->find();
			if(!$count){
				$_SESSION['NEED_ACTIVE']	=	1;

				if(APP_NAME.'/'.MODULE_NAME != 'home/Public'){
					redirect(U('home/Public/init',array('invitecode'=>$_SESSION['invitecode'])));
				}
			}

			return true;
		}
	}

	//注销本地登录
	public function logoutLocal($username){
		//return;
		//注销session
		unset($_SESSION['mid']);
		unset($_SESSION['uname']);
		unset($_SESSION['userInfo']);
		unset($_SESSION['AppUserInfo']);

		//注销cookie
		cookie($this->cookie_key,NULL);

		//跳转到登陆页面
		return true;
		//send_http_status('401');
	}

	/* 后台管理相关方法 */

	//启动服务，未编码
	public function _start(){
		return true;
	}

	//停止服务，未编码
	public function _stop(){
		return true;
	}

	//卸载服务，未编码
	public function _install(){
		return true;
	}

	//卸载服务，未编码
	public function _uninstall(){
		return true;
	}
}
?>